<template>
	<view class="container">
		<view class="o-plr">
			<view class="c-t65 c-tb o-ptb">请输入新手机号</view>
			<u-field
				v-model="phone"
				label="手机号"
				placeholder="请填写手机号"
				maxlength="11"
			>
			</u-field>
			<view class="l-fjb o-mt" style="margin-bottom: 360rpx;">
				<view class="">
					<u-field
						v-model="inputCode"
						label="验证码"
						placeholder="请填写验证码"
					>
					</u-field>
				</view>
				<view class="">
					<u-toast ref="uToast"></u-toast>
					<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" 
					@change="codeChange"></u-verification-code>
					<u-button @click="getCode" size="mini">{{tips}}</u-button>
				</view>
			</view>
			<u-button type="primary" :disabled="disabled" @click="goReplace()">确定</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: null,
				inputCode: null,
				code: null,
				tips: '',
				// refCode: null,
				seconds: 60,
				// disabled: true
			}
		},
		computed:{
			disabled() {
				return this.phone&&this.inputCode?false: true
			}
		},
		onLoad() {
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			
			//获取验证码
			getCode() {
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$store.dispatch('get_Code',{phone: this.phone})
					.then(res => {
						console.log('res ==>', res.data.data.code)
						if(res.data.code == '200') {
							this.code = res.data.data.code
							// setTimeout(() => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								this.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							// }, 2000);
						}
					})
					.catch(err => {
						uni.hideLoading();
						this.$Function.Toast('验证码获取失败');
						this.$refs.uCode.reset()
					})
					
					
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			//点击确定按钮
			goReplace() {
				if(!this.$Function.MobileVer(this.phone)){
					this.$Function.Toast('请输入正确的手机号')
					return
				}
				if(this.inputCode != this.code) {
					this.$Function.Toast('验证码不正确')
					return
				}
				this.$store.dispatch('change_Pone', {phone: this.phone,code: this.inputCode})
				.then(res => {
					console.log(res.data);
					if(res.data.code == '200') {
						this.$Function.Suc('操作成功');
						uni.setStorageSync('phone', this.phone);
						setTimeout(() => {this.$Function.LinkTo('','Back')},1000)
					}else if(res.data.code == '400') {
						this.$refs.uCode.reset()
					}
				})
			},
			
			end() {
				// this.$u.toast('倒计时结束');
			},
			start() {
				// this.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style>
</style>
